package vd

import (
	"fmt"
	"errors"
	"strings"
	"net/url"
)

type TypeId struct {
	TypeIdFuzzy
	Version  string
}
type TypeIdFuzzy struct {
	Vendor   string
	Project  string
	Name     string
}

type AdapterId struct {
	From  TypeId
	To    TypeId
}
type ValidatorId TypeId

func MakeTypeId(vendor string, project string, name string, version string) TypeId {
	return TypeId {
		TypeIdFuzzy: TypeIdFuzzy {
			Vendor:  vendor,
			Project: project,
			Name:    name,
		},
		Version:    version,
	}
}
func ParseTypeId(str string) (TypeId, error) {
	var bad = (func() (TypeId, error) {
		return TypeId {}, errors.New("bad service identifier")
	})
	var t = strings.Split(str, ":")
	if len(t) != 4 { return bad() }
	var vendor, err0 = url.QueryUnescape(t[0])
	if err0 != nil { return bad() }
	var project, err1 = url.QueryUnescape(t[1])
	if err1 != nil { return bad() }
	var name, err2 = url.QueryUnescape(t[2])
	if err2 != nil { return bad() }
	var version, err3 = url.QueryUnescape(t[3])
	if err3 != nil { return bad() }
	return TypeId {
		TypeIdFuzzy: TypeIdFuzzy {
			Vendor:  vendor,
			Project: project,
			Name:    name,
		},
		Version: version,
	}, nil
}
func (id TypeId) String() string {
	return fmt.Sprintf (
		"%s:%s:%s:%s",
		url.QueryEscape(id.Vendor),
		url.QueryEscape(id.Project),
		url.QueryEscape(id.Name),
		url.QueryEscape(id.Version),
	)
}


